package net.atomtree.handler;


/**
 * 
 * @author czk
 * @see http://spark.apache.org/docs/1.4.1/monitoring.html 通过此类将文档中的所有rest
 *      api的json数据读取出来
 */
public class RestHandler {
	/**
	 * 所有的方法都是拼接url 最终在这里调用restapi 得到json字符串
	 * @param url
	 * @return
	 * @throws Exception
	 */
	public static String getJson(String url) throws Exception{
		String json = HTTPRequestPoster.sendGetRequest(url);
		return json;
	}
	/**
	 *  /applications
	 *  A list of all applications
	 *  @param url rest 访问的master地址
	 *  @return 返回json格式的application字符串
	 *  @throws Exception url访问不到master或worker
	 */
	public static String getApplication(StringBuffer url) throws Exception{
		url=url.append("/applications/");
		return getJson(url.toString());
	}
	/**
	 * /applications/appname/
	 * jobs A list of all jobs for a given application
	 * @throws Exception 
	 */
	public static String getApplicationByappname(StringBuffer url,String appname) throws Exception{
		url=url.append("/applications/").append(appname);
		return getJson(url.toString());
	}
	/**
	 * 
	 * @param url
	 * @param appname
	 * @return
	 * @throws Exception
	 */
	public static String getApplicationJob(StringBuffer url,String appname) throws Exception{
		url=url.append("/applications/").append(appname).append("/jobs");
		System.out.print("jobUrl:"+url);
		return getJson(url.toString());
	}
	/**
	 * /applications/appname/jobs/[job-appname]	
	 * Details for the given job
	 * @throws Exception 
	 * 
 	 */
	public static String getApplicationJobByappname(StringBuffer url,String appname,String jobappname) throws Exception{
		url=url.append("/applications/").append(appname).append("/jobs/").append(jobappname);
		return getJson(url.toString());
	}
	/**
	 * /applications/appname/stages	
	 * A list of all stages for a given application
	 * @throws Exception 
	 */
	public static String getApplicationStages(StringBuffer url,String appname) throws Exception{
		url=url.append("/applications/").append(appname).append("/stages");
		return getJson(url.toString());
	}
	/**
	 * /applications/appname/stages/[stage-id]	
	 * A list of all attempts for the given stage	
	 * @throws Exception 
	 */
	public static String getApplicationStagesByAppappnameAndStageappname(StringBuffer url,String appname,String stageappname) throws Exception{
		url=url.append("/applications/").append(appname).append("/stages/").append(stageappname);
		return getJson(url.toString());
	}
	/**
	 * /applications/appname/stages/[stage-appname]/[stage-attempt-appname]/taskSummary	
	 * Summary metrics of all tasks in the given stage attempt
	 * @throws Exception 
	 */
	public static String getApplicationStagesTaskSummary(StringBuffer url,String appname,String stageappname,String attemptappname) throws Exception{
		url=url.append("/applications/").append(appname).append("/stages/").append(stageappname).append("/").append(attemptappname).append("/taskSummary");
		return getJson(url.toString());
	}
	/**
	 * /applications/appname/stages/[stage-appname]/[stage-attempt-appname]/taskList	
	 * A list of all tasks for the given stage attempt
	 * @throws Exception 
	 */
	public static String getApplicationStagesTaskList(StringBuffer url,String appname,String stageappname,String attemptappname) throws Exception{
		url=url.append("/applications/").append(appname).append("/stages/").append(stageappname).append("/").append(attemptappname).append("/taskList");
		return getJson(url.toString());
	}
	/**
	 * 	/applications/appname/executors	
	 *  A list of all executors for the given application
	 * @throws Exception 
	 */
	public static String getApplicationExecutor(StringBuffer url,String appname) throws Exception{
		url=url.append("/applications/").append(appname).append("/executors");
		return getJson(url.toString());
	}
	/**
	 * 	/applications/appname/storage/rdd	
	 *  A list of stored RDDs for the given application
	 * @throws Exception 
	 */
	public static String getApplicationRDD(StringBuffer url,String appname) throws Exception{
		url=url.append("/applications/").append(appname).append("/storage/rdd");
		return getJson(url.toString());
	}
	/**
	 * 	/applications/appname/storage/rdd/[rdd-appname]	
	 *  Details for the storage status of a given RDD
	 * @throws Exception 
	 */
	public static String getApplicationRDDByappname(StringBuffer url,String appname,String rddId) throws Exception{
		url=url.append("/applications/").append(appname).append("/storage/rdd/").append(rddId);
		return getJson(url.toString());
	}
}
